local qiaoen = fk.CreateSkill {
    name = "SP_heg__qiaoen",
}

Fk:loadTranslationTable {
    ["SP_heg__qiaoen"] = "巧恩",
    [":SP_heg__qiaoen"] = "结束阶段，若你本回合使用过的牌数等于X，你可以令两名势力相同的角色<a href='heg_swap_deputy'>交换副将</a>，" ..
    "若因此存在两张可以构成一组珠联璧合关系的武将牌且无“珠联璧合”标记的角色，你令其中一名角色回复1点体力（X为场上与你势力相同的角色数）。",

    ["#SP_heg__qiaoen-invoke"] = "巧恩：选择两名势力相同的角色交换副将",
    ["#SP_heg__qiaoen-choose"] = "巧恩：选择一名角色，令其回复1点体力",
    ["@SP_heg_qiaoen-record-turn"] = "巧恩",

    ["$SP_heg__qiaoen1"] = "妾蒲柳之姿，幸蒙君恩方化从龙之凤。",
    ["$SP_heg__qiaoen2"] = "尊位椒房、垂立九五，君之恩也、妾之幸也。",
}

local H = require "packages.ol_hegemony.util"

qiaoen:addEffect(fk.EventPhaseStart, {
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(qiaoen.name) and player.phase == Player.Finish
            and #player.room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
                return e.data.from == player
            end, Player.HistoryTurn) == #table.filter(player.room.alive_players, function(p)
                return H.compareKingdomWith(p, player)
            end)
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local _, ret = room:askToUseActiveSkill(player, {
            skill_name = "#SP_heg__qiaoen_choose&",
            skip = true,
            extra_data = {skillName = qiaoen.name},
            prompt = "#SP_heg__qiaoen-invoke",
            cancelable = true,
        })
        if ret then
            event:setCostData(self, { tos = ret.targets })
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to1 = event:getCostData(self).tos[1]
        local to2 = event:getCostData(self).tos[2]
        H.SwapTwoPlayersDeputy(room, to1, to2)
        if to1.dead or to2.dead then return end
        local targets = {}
        if Fk.generals[to1.general]:isCompanionWith(Fk.generals[to2.deputyGeneral]) then
            table.insert(targets, to1)
        end
        if Fk.generals[to2.general]:isCompanionWith(Fk.generals[to1.deputyGeneral]) then
            table.insert(targets, to2)
        end
        if #targets == 0 then return end
        local to = room:askToChoosePlayers(player,{
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#SP_heg__qiaoen-choose",
            skill_name = qiaoen.name,
            cancelable = false,
        })
        if #to > 0 then
            room:recover({
                who = to[1],
                num = 1,
                skillName = qiaoen.name,
            })
        end
    end,
})

qiaoen:addEffect(fk.CardUsing,{
  can_refresh = function(self, event, target, player, data)
     return target == player and player.phase ~= Player.NotActive and player:hasSkill(qiaoen.name)
  end,
  on_refresh = function(self, event, target, player, data)
      local room = player.room
      room:addPlayerMark(player,"SP_heg_qiaoen-record-turn",1)
      if player:hasShownSkill(qiaoen.name) then
        room:setPlayerMark(player,"@SP_heg_qiaoen-record-turn", player:getMark("SP_heg_qiaoen-record-turn"))
      end
  end,
})

return qiaoen
